﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Lib.Common;
using System.Linq.Dynamic;
using SDT.Lib.Paging;
using SDT.Admin.ViewModels;
using SDT.Lib.Helpers;

namespace SDT.Admin.Controllers
{
    [RoleHelper(Roles = RoleSystem.Question)]
    public class AnswerController : Controller
    {
        //
        // GET: /Answer/
        SDTDataContext eDataContext = new SDTDataContext();
        public ActionResult Index()
        {
            try
            {
                string order = UserState.getUserStateFromRequest("answer.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("answer.orderDir", "orderDir", "desc").ToString();

                string keyword = UserState.getUserStateFromRequest("answer.Keyword", "Keyword", "").ToString();
                var catid = Utils.ParseInt(UserState.getUserStateFromRequest("answer.Type", "ddlCategory", 0));

                var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("answer.PageNumber", "page", 1));
                var pageIndex = pageNumber - 1;
                var pageSize = Convert.ToInt32(UserState.getUserStateFromRequest("answer.PageSize", "pageSize", 20));
                AnswerIndexModels an = new AnswerIndexModels();
                an.Answers = eDataContext.Answers
                    .Where(a => catid > 0 ? a.Question.CatID == catid : true)
                    .Where(a => string.IsNullOrEmpty(keyword) ? true :
                        a.AnswerName.ToLower().Contains(keyword.ToLower().Trim()))
                    .OrderBy(string.Format("{0} {1}", order, orderDir))
                    .ToPagedList(pageIndex, pageSize);

                ViewData["orderDir"] = orderDir;
                ViewData["order"] = order;
                an.Categories = eDataContext.QuestionCategories.ToList();
                an.Category = catid > 0 ? eDataContext.QuestionCategories.Where(e => e.ID == catid).FirstOrDefault() : null;
                return View(an);
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Answer/Details/5

        public ActionResult Details(int id)
        {
            var a = eDataContext.Answers.Where(e => e.ID == id).SingleOrDefault();
            return View(a);
        }
        //
        // GET: /Answer/Edit/5
 
        public ActionResult Edit(int id)
        {
            var a = eDataContext.Answers.Where(e => e.ID == id).SingleOrDefault();
            return View(a);
        }

        //
        // POST: /Answer/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, Answer m)
        {
            try
            {
                m = eDataContext.Answers.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(m);
                m.ModifyBy = UserManager.GetUserId();
                m.ModifyDate = DateTime.Now;
                m.PubYN= Request["chkPubYN"]!="false"?true:false;
                m.Question.nPubAnswers = m.PubYN == true ? m.Question.nPubAnswers += 1 : (m.Question.nPubAnswers > 0 ? m.Question.nPubAnswers -= 1 : 0);
                eDataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception exp)
            {
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }

        //
        // GET: /Answer/Delete/5
 
        public ActionResult Delete(int id)
        {
            var a = eDataContext.Answers.Where(e => e.ID == id).SingleOrDefault();
            return View(a);
        }

        //
        // POST: /Answer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                 var a = eDataContext.Answers.Where(e => e.ID == id).SingleOrDefault();
                 eDataContext.Answers.DeleteOnSubmit(a);
                 eDataContext.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
